<?php

use console\db\Migration;
use mdm\admin\components\Configs;

/**
 * Migrate创建初始化基础
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m150104_091300_t_init extends Migration {

    /**
     * 后台管理员
     * @todo create table `auth_user`.
     */
    public function buildTableAuthUser() {
        $config = Configs::instance();
        return [
            'name' => $config->userTable,
            'comment' => '系统管理员表',
            'columns' => [
                'id' => $this->primaryKey(),
                'username' => $this->string(50)->notNull()->unique()->comment('帐号名'),
                'mobile' => $this->char(32)->unique()->comment('手机号'), //'mobile' => $this->char(11)->notNull()->unique()->comment('手机号'),
                'avatar' => $this->char(255)->defaultValue('')->comment('头像'),
                //
                'auth_key' => $this->string(32)->notNull()->comment('加密密钥'),
                'password_hash' => $this->char(255)->notNull()->comment('HASH密码'),
                'password_reset_token' => $this->char(255)->unique()->comment('密码重置TOKEN'),
                'access_token' => $this->char(255)->comment('AccessToken'),
                'expired_at' => $this->unixTimestamp()->comment('Token过期时间'),
                //
                'realname' => $this->string(50)->notNull()->comment('真实姓名'),
                'gender' => $this->tinyint(1)->notNull()->unsigned()->defaultValue(0)->comment('性别:0未知，1男，2女'),
                'mobile_hash' => $this->char(255)->comment('加密手机号'),
                'email' => $this->string(64)->comment('邮箱'),
                'profile' => $this->string(255)->comment('个人简介'),
                'ip' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('最后登录IP'),
                'company_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('隶属单位ID'),
                'root_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('根ID'),
                //
                'error_times' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('登录失败次数'),
                'validated_at' => $this->unixTimestamp()->comment('校验时间'),
                //
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('注册时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('注销时间'),
            ]
        ];
    }

    /**
     * 后台管理菜单表
     * @return array
     */
    public function buildTableAuthMenu() {
        $config = Configs::instance();
        return [
            'name' => $config->menuTable,
            'comment' => '后台管理菜单表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(128)->notNull()->comment('名称'),
                'icon' => $this->char(128)->comment('图标'),
                'parent' => $this->integer(11)->comment('父级菜单'),
                'route' => $this->char(255)->comment('路由'),
                'order' => $this->integer(11)->comment('排序'),
                'data' => $this->binary()->comment('扩展数据'),
                'visibled' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(1)->comment('是否可见：0隐藏，1可见'),
                "FOREIGN KEY ([[parent]]) REFERENCES {$config->menuTable}([[id]]) ON DELETE SET NULL ON UPDATE CASCADE",
            ]
        ];
    }

}
